/**
 * 获取二维码接口
 */

//引入Q模块
let Q = require('q');

//引入公共函数
let util = require('../../util');

let ccap = require('ccap');

//login入口函数
let index = function(getData, postData, config, redis) {
	let deferred = Q.defer();
	if(getData.rn) {
		let Vcode = ccap();
		Vcode = Vcode.get();
		console.log('生成验证码', Vcode[0]);
		console.log('请求随机值', getData.rn);
		redis.set('vcode-' + getData.rn, Vcode[0]).then(function() {
			return redis.expire('vcode-' + getData.rn, 300);
		}, function() {
			deferred.reject('-0138');
		}).then(function() {
			deferred.resolve(['file', {
				contentType: 'image/jpeg',
				fileContent: Vcode[1],
				base64: true
			}]);
		}, function() {
			deferred.reject('-0139');
		});
	}
	else {
		deferred.reject('-0137');
	}
	return deferred.promise;
}

module.exports = {
	index: index
};